package com.qfedu.demo.springcloud.stream;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageChannel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableBinding({Source.class, QfOutputChannel.class}) // 绑定发射器
@RestController
public class StreamProducer {

	@Resource
	private Source source;// 注入默认频道的发送器
	
	@GetMapping("/{msg}")
	String sendMsg (@PathVariable("msg") String msg) {
		// 通过发射器，获取频道，然后发送消息
		source.output().send(MessageBuilder.withPayload(msg).build());
		return msg;
	}
	
	@Resource
	@Qualifier("qfOutputChannel") // 指定被注入bean的名称
	private MessageChannel qfOutputChannel;

	@GetMapping("/2/{msg}")
	String sendMsg2 (@PathVariable("msg") String msg) {
		// 通过频道，发送消息
		qfOutputChannel.send(MessageBuilder.withPayload(msg).build());
		return msg;
	}
	
	public static void main(String[] args) {
		SpringApplication.run(StreamProducer.class, args);
	}

}
